package com.cn.query;

import com.cn.client.SearchClient;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.junit.Test;

import java.io.IOException;

public class CatDemo {

  @Test
  public void searchAllIndices() {
    try {
      IndicesClient indices = SearchClient.client().indices();
      GetIndexResponse getIndexResponse = indices.get(new GetIndexRequest("*"), RequestOptions.DEFAULT);
      String[] indices1 = getIndexResponse.getIndices();
      for (String s : indices1) {
        System.out.println(s);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  /**
   * 获取所有索引
   */
  @Test
  public void searchAllIndices2() {
    try {
      Response response = SearchClient.client().getLowLevelClient().performRequest(new Request("get", "/_cat/indices?format=json&h=index"));
      if (response.getStatusLine().getStatusCode() == 200) {
        String strResult = EntityUtils.toString(response.getEntity());
        System.out.println(strResult);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

}
